/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Collection;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import type.UserTitle;
import util.Utils;

/**
 *
 * @author wei
 */
@Entity(name = "SystemUser")
public class SystemUserEntity extends PersonEntity implements Serializable {

    @ManyToOne
    private DepartmentEntity department;
    @OneToOne(mappedBy = "manager")
    private DepartmentEntity managesDepartment;
    @OneToOne(mappedBy = "systemUser", cascade = CascadeType.ALL)
    private UserSettingEntity setting = new UserSettingEntity();
    @OneToMany(targetEntity = PurchaseOrderEntity.class, mappedBy = "systemUser")
    private Collection<PurchaseOrderEntity> purchaseOrder = new ArrayList<PurchaseOrderEntity>();
    @OneToMany(mappedBy = "systemUser")
    private Collection<SalesOrderEntity> salesOrder = new HashSet<SalesOrderEntity>();
    @OneToMany(mappedBy = "systemUser")
    private Collection<SalesInquiryEntity> salesInquiry = new HashSet<SalesInquiryEntity>();
    @OneToMany(mappedBy = "systemUser")
    private Collection<SalesQuotationEntity> salesQuotation = new HashSet<SalesQuotationEntity>();
    @OneToMany(mappedBy = "sender")
    private Collection<InternalMessageEntity> messagesSent = new HashSet<InternalMessageEntity>();
    @ManyToMany(mappedBy = "recipients")
    private Collection<InternalMessageEntity> messagesReceived = new HashSet<InternalMessageEntity>();
    @OneToMany(mappedBy = "customerExecutive")
    private List<CustomerEntity> customers;    
    @OneToMany(mappedBy = "receiver")
    private Collection<InternalMessageReceivedEntity> messagesReceivedInstances = new HashSet<InternalMessageReceivedEntity>();

    public List<CustomerEntity> getCustomers() {
        return customers;
    }

    public void setCustomers(List<CustomerEntity> customers) {
        this.customers = customers;
    }

    public SystemUserEntity() {
        super();
    }

    public SystemUserEntity(String name, String password, String firstName, String lastName, UserTitle title) {
        super(name, password, firstName, lastName, title);
    }

    public DepartmentEntity getDepartment() {
        return department;
    }

    public void setDepartment(DepartmentEntity department) {
        this.department = department;
    }

    /**
     * @return the salesOrder
     */
    public Collection<SalesOrderEntity> getSalesOrder() {
        return salesOrder;
    }

    /**
     * @param salesOrder the salesOrder to set
     */
    public void setSalesOrder(Collection<SalesOrderEntity> salesOrder) {
        this.salesOrder = salesOrder;
    }

    /**
     * @return the salesInquiry
     */
    public Collection<SalesInquiryEntity> getSalesInquiry() {
        return salesInquiry;
    }

    /**
     * @param salesInquiry the salesInquiry to set
     */
    public void setSalesInquiry(Collection<SalesInquiryEntity> salesInquiry) {
        this.salesInquiry = salesInquiry;
    }

    /**
     * @return the salesQuotation
     */
    public Collection<SalesQuotationEntity> getSalesQuotation() {
        return salesQuotation;
    }

    /**
     * @param salesQuotation the salesQuotation to set
     */
    public void setSalesQuotation(Collection<SalesQuotationEntity> salesQuotation) {
        this.salesQuotation = salesQuotation;
    }

    public Collection<InternalMessageEntity> getMessagesReceived() {
        return messagesReceived;
    }

    public void setMessagesReceived(Collection<InternalMessageEntity> messagesReceived) {
        this.messagesReceived = messagesReceived;
    }

    public Collection<InternalMessageEntity> getMessagesSent() {
        return messagesSent;
    }

    public void setMessagesSent(Collection<InternalMessageEntity> messagesSent) {
        this.messagesSent = messagesSent;
    }

    public DepartmentEntity getManagesDepartment() {
        return managesDepartment;
    }

    public void setManagesDepartment(DepartmentEntity managesDepartment) {
        this.managesDepartment = managesDepartment;
    }

    public UserSettingEntity getSetting() {
        return setting;
    }

    public void setSetting(UserSettingEntity setting) {
        this.setting = setting;
    }

    public Collection<InternalMessageReceivedEntity> getMessagesReceivedInstances() {
        return messagesReceivedInstances;
    }

    public void setMessagesReceivedInstances(Collection<InternalMessageReceivedEntity> messagesReceivedInstances) {
        this.messagesReceivedInstances = messagesReceivedInstances;
    }

    @Override
    public String toString() {
        return this.getFirstName() + " " + this.getLastName() + " [Id= " + this.getId() + "]";
    }

    public boolean isUserIsAdmin() {
//        System.out.println("kkkkkkk"); Please put meaningful debug output!
        boolean inrole = false;
        Collection<RoleEntity> Groups = getRoles();
        String[] groups = new String[Groups.size()];
        int i = 0;
        for (RoleEntity rg : Groups) {
            groups[i] = rg.getRoleName();
            FacesContext facesContext = FacesContext.getCurrentInstance();
            Utils.log(facesContext, "User: " + getName() + " Group: " + rg.getRoleName());
//            System.out.println("kkkkkkk");

            if (groups[i].equalsIgnoreCase("admin")) {
                Utils.log(facesContext, groups[i]);
                inrole = true;
                break;
            }
            i++;
        }
        return inrole;
    }

    public Collection<PurchaseOrderEntity> getPurchaseOrder() {
        return purchaseOrder;
    }

    public void setPurchaseOrder(Collection<PurchaseOrderEntity> purchaseOrder) {
        this.purchaseOrder = purchaseOrder;
    }
    
    public String rootToString() {
        return "SystemUserEntity{" + "department=" + department + ", managesDepartment=" + managesDepartment + ", setting=" + setting + ", purchaseOrder=" + purchaseOrder + ", salesOrder=" + salesOrder + ", salesInquiry=" + salesInquiry + ", salesQuotation=" + salesQuotation + ", messagesSent=" + messagesSent + ", messagesReceived=" + messagesReceived + ", customers=" + customers + ", messagesReceivedInstances=" + messagesReceivedInstances + '}';
    }

}
